package com.sqfw.project.wpReport.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.sqfw.framework.web.page.PageDomain;
import com.sqfw.framework.web.page.TableSupport;
import com.sqfw.project.wpReport.domain.SeatEvaluateReport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.sqfw.framework.aspectj.lang.annotation.Log;
import com.sqfw.framework.aspectj.lang.enums.BusinessType;
import com.sqfw.project.wpReport.domain.SupervisionWorkReport;
import com.sqfw.project.wpReport.service.ISupervisionWorkReportService;
import com.sqfw.framework.web.controller.BaseController;
import com.sqfw.common.utils.poi.ExcelUtil;
import com.sqfw.framework.web.page.TableDataInfo;

/**
 * 督导工作情况汇总表Controller
 * 
 * @author lx
 * @date 2023-07-28
 */
@RestController
@RequestMapping("/wpReport/supervisionWorkReport")
public class SupervisionWorkReportController extends BaseController
{
    @Autowired
    private ISupervisionWorkReportService supervisionWorkReportService;

    /**
     * 查询督导工作情况汇总表列表
     */
    @PreAuthorize("@ss.hasPermi('getSupervisionWorkReportList')")
    @GetMapping("/getSupervisionWorkReportList")
    public TableDataInfo getSupervisionWorkReportList(SupervisionWorkReport supervisionWorkReport)
    {
        startPage();
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer pageNum = pageDomain.getPageNum();
        Integer pageSize = pageDomain.getPageSize();
        TableDataInfo tableDataInfo = supervisionWorkReportService.getSupervisionWorkReportList(supervisionWorkReport);

        long total = tableDataInfo.getTotal();
        if ((long) pageNum * pageSize >= total && total > 0) {
            List<SupervisionWorkReport> list = (List<SupervisionWorkReport>) tableDataInfo.getRows();
            if (list.size()>0){
                TableDataInfo tableDataInfoCon = supervisionWorkReportService.getSupervisionWorkReportList(supervisionWorkReport);
                List<SupervisionWorkReport> reportList = (List<SupervisionWorkReport>) tableDataInfoCon.getRows();
                SupervisionWorkReport report = new SupervisionWorkReport();

                report.setCode("总计");
                //督导审核数
                int ddExamine = reportList.stream()
                        .map(SupervisionWorkReport::getDdExamine)
                        .reduce(Integer::sum)
                        .orElse(0);
                report.setDdExamine(ddExamine);
                //督导退回坐席
                int ddToZx = reportList.stream()
                        .map(SupervisionWorkReport::getDdToZx)
                        .reduce(Integer::sum)
                        .orElse(0);
                report.setDdToZx(ddToZx);
                //审核人退回督导
                int shrToDd = reportList.stream()
                        .map(SupervisionWorkReport::getShrToDd)
                        .reduce(Integer::sum)
                        .orElse(0);
                report.setShrToDd(shrToDd);
                //质检审核数
                int selfCount = reportList.stream()
                        .map(SupervisionWorkReport::getSelfCount)
                        .reduce(Integer::sum)
                        .orElse(0);
                report.setSelfCount(selfCount);

                //质检退回坐席数
                int zjToZx = reportList.stream()
                        .map(SupervisionWorkReport::getZjToZx)
                        .reduce(Integer::sum)
                        .orElse(0);
                report.setZjToZx(zjToZx);

                //过程不明晰
                int textCaseCount = reportList.stream()
                        .map(SupervisionWorkReport::getTextCaseCount)
                        .reduce(Integer::sum)
                        .orElse(0);
                report.setTextCaseCount(textCaseCount);

                //培训助理审核数
                int trainCount = reportList.stream()
                        .map(SupervisionWorkReport::getTrainCount)
                        .reduce(Integer::sum)
                        .orElse(0);
                report.setTrainCount(trainCount);

                //查无纠错发起数
                int cwSendCount = reportList.stream()
                        .map(SupervisionWorkReport::getCwSendCount)
                        .reduce(Integer::sum)
                        .orElse(0);
                report.setCwSendCount(cwSendCount);


                //查无纠错审核数
                int cwProcessCount = reportList.stream()
                        .map(SupervisionWorkReport::getCwProcessCount)
                        .reduce(Integer::sum)
                        .orElse(0);
                report.setCwProcessCount(cwProcessCount);

                //总得分
                int soundCaseCount = reportList.stream()
                        .map(SupervisionWorkReport::getSoundCaseCount)
                        .reduce(Integer::sum)
                        .orElse(0);
                report.setSoundCaseCount(soundCaseCount);

                list.add(report);

            }

        }
        return tableDataInfo;
    }

    /**
     * 导出督导工作情况汇总表列表
     */
    @Log(title = "督导工作情况汇总表", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('supervisionWorkReport:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, SupervisionWorkReport supervisionWorkReport)
    {
        TableDataInfo tableDataInfo = supervisionWorkReportService.getSupervisionWorkReportList(supervisionWorkReport);
        List<SupervisionWorkReport> list = (List<SupervisionWorkReport>) tableDataInfo.getRows();
        if (list.size()>0){
            SupervisionWorkReport report = new SupervisionWorkReport();

            report.setCode("总计");
            //督导审核数
            int ddExamine = list.stream()
                    .map(SupervisionWorkReport::getDdExamine)
                    .reduce(Integer::sum)
                    .orElse(0);
            report.setDdExamine(ddExamine);
            //督导退回坐席
            int ddToZx = list.stream()
                    .map(SupervisionWorkReport::getDdToZx)
                    .reduce(Integer::sum)
                    .orElse(0);
            report.setDdToZx(ddToZx);
            //审核人退回督导
            int shrToDd = list.stream()
                    .map(SupervisionWorkReport::getShrToDd)
                    .reduce(Integer::sum)
                    .orElse(0);
            report.setShrToDd(shrToDd);
            //质检审核数
            int selfCount = list.stream()
                    .map(SupervisionWorkReport::getSelfCount)
                    .reduce(Integer::sum)
                    .orElse(0);
            report.setSelfCount(selfCount);

            //质检退回坐席数
            int zjToZx = list.stream()
                    .map(SupervisionWorkReport::getZjToZx)
                    .reduce(Integer::sum)
                    .orElse(0);
            report.setZjToZx(zjToZx);

            //过程不明晰
            int textCaseCount = list.stream()
                    .map(SupervisionWorkReport::getTextCaseCount)
                    .reduce(Integer::sum)
                    .orElse(0);
            report.setTextCaseCount(textCaseCount);

            //培训助理审核数
            int trainCount = list.stream()
                    .map(SupervisionWorkReport::getTrainCount)
                    .reduce(Integer::sum)
                    .orElse(0);
            report.setTrainCount(trainCount);

            //查无纠错发起数
            int cwSendCount = list.stream()
                    .map(SupervisionWorkReport::getCwSendCount)
                    .reduce(Integer::sum)
                    .orElse(0);
            report.setCwSendCount(cwSendCount);


            //查无纠错审核数
            int cwProcessCount = list.stream()
                    .map(SupervisionWorkReport::getCwProcessCount)
                    .reduce(Integer::sum)
                    .orElse(0);
            report.setCwProcessCount(cwProcessCount);

            //总得分
            int soundCaseCount = list.stream()
                    .map(SupervisionWorkReport::getSoundCaseCount)
                    .reduce(Integer::sum)
                    .orElse(0);
            report.setSoundCaseCount(soundCaseCount);

            list.add(report);

        }
        ExcelUtil<SupervisionWorkReport> util = new ExcelUtil<SupervisionWorkReport>(SupervisionWorkReport.class);
        util.exportExcel(response, list, "督导工作情况汇总表数据");
    }


}
